/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

int get_link_len(struct ListNode * head) {
    int len = 0;
    struct ListNode *p = head;
    while(p != NULL) {
        len++;
        p = p->next;
    }
    return len;
}

struct ListNode* rotateRight(struct ListNode* head, int k){
    int len = get_link_len(head);
    struct ListNode *t = NULL, *h = head;
    int counter = 0;
    if(len == 0) {
        return NULL;
    }
    if(k == 0 || len == 1 || k % len == 0) {
        return head;
    }
    k = k % len;
    while(h != NULL) {
        if(counter == len - k - 1) {
            printf("%d\n", h->val);
            t = h->next;
            h->next = NULL;
            break;
        }
        h = h->next;
        counter++;
    }
    h = t;
    while(h->next != NULL) {
        h = h->next;
    }
    h->next = head;
    head = t;
    return head;
}